<template>

  <div class="login_contair">
    <!-- 头像区域 -->
    <div class="avatar_box">
      <img src="../assets/img/logo.png" >
    </div>

    <!-- 登录区域 -->
    <el-form :model="formData" ref="loginFormRef" class="form_box">
      <el-form-item
        prop="username"
        :rules="[
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ]"
      >
        <el-input placeholder="请输入账号" prefix-icon="el-icon-user-solid" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item
        prop="password"
        :rules="[
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ]"
      >
        <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="formData.password" show-password></el-input>
      </el-form-item>
      <el-form-item class="btn">
        <el-button :plain="true" type="primary" @click="login()"> 登 录 </el-button>
        <el-button type="info" @click="resetForm()"> 重 置 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    name:'Login',
    data(){
      return {
        formData:{
          username:'admin',
          password:'123456'
        }
      }
    },
    methods:{
      resetForm(){
        this.$refs.loginFormRef.resetFields();
      },
      login(){
        this.$refs.loginFormRef.validate(async valid =>{
          if(!valid) return;
          const {data} = await this.axios.post("login",this.formData);
          if(data.meta.status !== 200){
            this.$message.error('账号或密码错误，请输入正确后再登录');
          }else{
            this.$message({
              message: '登陆成功~',
              type: 'success'
            });
          }
          sessionStorage.setItem('token',data.data.token);
          this.$router.push({name:'home'});
        })
      },
      
    }
}
</script>

<style lang="less" scoped>

.login_contair{
  width: 450px;
  height: 300px;
  border-radius: 5px;
  opacity: 0.8;
  background: #ffff;
  /* 垂直水平居中一
  margin:auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;*/
  

  // 垂直水平居中二
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);

  /*垂直水平居中三 在其父元素上开启
  display: flex;
  align-items: center;
  justify-content: center;*/

  .avatar_box{ 
    width: 130px;
    height: 130px;
    border:1px solid #ededed;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #dddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #ffff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeee;
    }
  }
}
.form_box{
  width: 100%;
  position: absolute;
  bottom: 12px;
  padding: 0 20px;
  box-sizing: border-box;
}
.btn{
  display: flex;
  justify-content: flex-end;
}
</style>